<?php

class Users_Model_Access_Role_Storage_Table extends Workset_Model_Object_Storage_Table_Abstract {

	protected $_objectClass 		= 'Users_Model_Access_Role';

	protected $_tableClass 			= 'Users_Table_Access_Role';

	protected $_iteratorClass 		= 'Workset_Model_Object_Iterator';

    protected $_ruleObjectClass = 'Users_Model_Access_Rule';

    protected $_ruleTableClass = 'Users_Table_Access_Rule';

    protected $_groupTableClass = 'Users_Table_Access_Group';

    protected $_role2GroupTableClass = 'Users_Table_Access_Role2Group';

    function addRule(Users_Model_Access_Role $role,
					 Users_Model_Access_Resource $resource,
					 Users_Model_Access_Privilege $privilege) {

		$row = $role->getData();
		if ($row instanceof Workset_Db_Table_Row) {
			return $row->addDependentRow($this->_ruleTableClass, array(
                'resource_id' => $resource->getId(),
				'privilege_id' => $privilege->getId()
            ));
		}
	}

	function deleteRule(Users_Model_Access_Role $role,
					 Users_Model_Access_Resource $resource,
					 Users_Model_Access_Privilege $privilege) {
		$row = $role->getData();
		if ($row instanceof Workset_Db_Table_Row) {
			$subrow = $row->findDependentRow($this->_ruleTableClass, 'role', array(
                'resource_id = ?' => $resource->getId(),
                'privilege_id = ?' => $privilege->getId(),
            ));
            if ($subrow) {
                return $subrow->delete();
            }
		}
	}

	function getRules(Users_Model_Access_Role $role) {
		$row = $role->getData();
		if ($row instanceof Workset_Db_Table_Row) {
			$rowset = $row->findDependentRowset($this->_ruleTableClass, 'role');

			return $this->getIterator($rowset, array(
                'countRows' => $this->_countAllRecords,
                'objectClass' => $this->_ruleObjectClass
            ));
		}
	}

    function getGroups(Users_Model_Access_Role $role) {
        $row = $role->getData();
		if ($row instanceof Workset_Db_Table_Row) {
			$rowset = $row->findManyToManyRowset($this->_groupTableClass, $this->_role2GroupTableClass, 'role');

			return $this->getIterator($rowset, array(
                'countRows' => $this->_countAllRecords,
                'objectClass' => $this->_ruleObjectClass
            ));
		}
    }
}

?>